package virtualadvisor;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.List;

public class ProfileWizard extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textField_EnterName;
	private JComboBox comboBox_SelectMinor;
	private JComboBox comboBox_SelectMajor;
	private String[] majorList = {"", "Computer Science Major (CMSC)", "Information Systems Major (IS)"};
	private String[] minorList = {"", "Computer Science Minor (CMSC)"};
	private Profile mainProfile;

	/**
	 * Launch the Profile Wizard application.
	 */
	public static void main(String[] args) {
		try {
			
			Profile p = new Profile();
			ProfileWizard dialog = new ProfileWizard(p);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ProfileWizard(final Profile profile) {	
		this.mainProfile = profile; 
		setTitle("Virtual Advisor - Profile Wizard");
		setResizable(false);
		setBounds(100, 100, 450, 228);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label_ProfileWizardTitle = new JLabel("Profile Wizard");
		label_ProfileWizardTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		label_ProfileWizardTitle.setBounds(10, 10, 180, 29);
		contentPanel.add(label_ProfileWizardTitle);

		JLabel label_EnterName = new JLabel("Enter Name");
		label_EnterName.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_EnterName.setBounds(10, 60, 83, 14);
		contentPanel.add(label_EnterName);

		textField_EnterName = new JTextField();
		textField_EnterName.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
			}
		});
		
		textField_EnterName.setColumns(10);
		textField_EnterName.setBounds(96, 54, 258, 20);
		contentPanel.add(textField_EnterName);
		
		JLabel label_SelectMajor = new JLabel("Select Major");
		label_SelectMajor.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_SelectMajor.setBounds(10, 99, 83, 14);
		contentPanel.add(label_SelectMajor);
		
		
		final JComboBox comboBox_SelectMajor = new JComboBox(majorList);
		comboBox_SelectMajor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(comboBox_SelectMajor.getSelectedItem().equals("Computer Science Major (CMSC)")){
					comboBox_SelectMinor.setEnabled(false);
				} else {
					comboBox_SelectMinor.setEnabled(true);
				}
			}
		});
		comboBox_SelectMajor.setBounds(97, 91, 257, 22);
		contentPanel.add(comboBox_SelectMajor);
		
		JLabel label_SelectMinor = new JLabel("Select Minor");
		label_SelectMinor.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_SelectMinor.setBounds(10, 138, 83, 14);
		contentPanel.add(label_SelectMinor);
		
		comboBox_SelectMinor = new JComboBox(minorList);
		comboBox_SelectMinor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		comboBox_SelectMinor.setBounds(97, 130, 258, 22);
		contentPanel.add(comboBox_SelectMinor);
		
		JLabel label_Optional = new JLabel("(Optional)");
		label_Optional.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_Optional.setBounds(363, 138, 69, 14);
		contentPanel.add(label_Optional);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton Button_CreateProfile = new JButton("Create Profile");
		Button_CreateProfile.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				String name = textField_EnterName.getText();
				if(name.equals("")){
					JOptionPane.showMessageDialog(contentPanel,
							"Please Enter Your Name.",
							"Warning", JOptionPane.WARNING_MESSAGE);
				} else {
					//System.out.println("Name: " + name);
					String majorSelected = (String) comboBox_SelectMajor.getSelectedItem();
					if(majorSelected.equals("")){
						JOptionPane.showMessageDialog(contentPanel,
							"Please Enter A Major.",
							"Warning", JOptionPane.WARNING_MESSAGE);
					} else {
						//System.out.println(majorSelected);
						String minorSelected = (String)comboBox_SelectMinor.getSelectedItem();
						//System.out.println(minorSelected);
				        
						//TODO Add name, majorSelected, minorSelected to Save Profile Information
						List<String> lMaj = new LinkedList<String>();
						lMaj.add(majorSelected);
						Student s = new Student(name, lMaj, minorSelected);

						mainProfile = new Profile(s);
						setVisible(false);
						dispose();
						mainProfile.saveProfileXML();
						MainGUI window = new MainGUI(mainProfile);
						window.initialize();
						window.frame_MainGui.setVisible(true);
					}
				}
			}
		});
		
		Button_CreateProfile.setActionCommand("Cancel");
		buttonPane.add(Button_CreateProfile);
	}
}
